﻿@using Model.Util
@using Model.WXshopBuiness
@using Xproject.Util
@{
    Layout = null;
    List<CategoryAttrGroups> groups = (List<CategoryAttrGroups>)ViewData["grooup"];
    List<CategoryAttr> attrs = (List<CategoryAttr>)ViewData["attrs"];
    List<CategoryAttrValues> attrvalues = (List<CategoryAttrValues>)ViewData["attrvalues"];
    List<int> existsAttrValueIds = (List<int>)ViewData["existsAttrValueIds"];
    Dictionary<int, string> inputValue = (Dictionary<int, string>)ViewData["inputValue"];
    Layout = "~/Views/Shared/_JSlayout.cshtml";
    string Domain = CommonHelper.GetAppSettings("Domin");
}

<style type="text/css">
    .attrName {
        display: inline-block;
        float: left;
        width: 100px;
    }
    .attrValue {
        display: inline-block;
    }
</style>
<div >
    <div class="  animated fadeInRight">
        <div class="row">
            @foreach (CategoryAttrGroups cag in groups)
            {
                List<CategoryAttr> tempattrs = attrs.Where(i =>i.AttrGroupId==cag.AttrGroupId).ToList();
                
                <div class="col-sm-12">
                    <div class="gray-bg">
                        <label>@cag.Name</label>
                    </div>
                    @if (tempattrs != null && tempattrs.Count > 0)
                    {
                        foreach (CategoryAttr ca in tempattrs)
                        {
                            List<CategoryAttrValues> tvals = attrvalues.Where(i => i.AttrId == ca.AttrId).ToList();

                         <div style="display:block;margin-left:20px;" class="AttrListFlag">
                            <div class="attrName">
                                @ca.Name:
                            </div>
                            <div class="attrValue attrValueListFlag" >
                                @foreach (CategoryAttrValues cav in tvals)
                                {
                                    if (cav.Isinput == 1)
                                    {
                                        <input type="radio" isinput="@cav.Isinput" name="attrValueName@(cav.AttrId)"  data-CatGroupid="@(cag.AttrGroupId)" data-CatAttrId="@(ca.AttrId)"  value="@(cav.AttrValueId)"  @(existsAttrValueIds.Contains(cav.AttrValueId)?"checked":"")  />@cav.AttrValue
                                        <input placeholder="手动输入值" id="manualinput@(cav.AttrValueId)" value="@(existsAttrValueIds.Contains(cav.AttrValueId)?inputValue[cav.AttrValueId]:"")"  />
                                    }
                                    else
                                    {
                                        <input type="radio" isinput="@cav.Isinput" name="attrValueName@(cav.AttrId)" data-CatGroupid="@(cag.AttrGroupId)" data-CatAttrId="@(ca.AttrId)"  value="@(cav.AttrValueId)"  @(existsAttrValueIds.Contains(cav.AttrValueId)?"checked":"") />@cav.AttrValue
                                    }

                                 }
                            </div>
                          </div>
                        }
                    }
                    else {
                        <div style="display:block;margin-left:20px;">
                            <label>此属性组下没有属性，请去分类管理添加属性和属性值</label>
                        </div>
                    }
                </div>
            }

        </div>
    
        <div class="row">
            
            <div class="col-lg-12" style="text-align:center"> <button onclick="editProductAttr('@(ViewBag.productId)')" class="form-control"> 提交</button></div>
            
           
        </div>
    </div>
</div>

<script type="text/javascript">
    function editProductAttr(productid) {
        var list = [];
        $(".AttrListFlag").each(
            function (index, i) {
                var self = $(this);
                self.find("input").each(
                    function (tindex, ti) {
                        var self = $(this);
                        if (self.is(':checked')) {
                            var data = new Object();
                            data.ProductId = productid;
                            data.AttrId = self.attr("data-CatAttrId");
                            data.IsInput = self.attr("isinput");
                           data.AttrValueId = self.val();
                           if (data.IsInput == "1") {
                               data.Inputvalue = $("#manualinput" + data.AttrValueId).val();
                           } else {
                               data.Inputvalue = '';
                           }

                           list.push(data);
                       }
                    }
                );
            }
        );

        if (list.length <= 0) {
            layer.alert('没有为产品添加属性！请选择', { icon: 1, skin: 'layer-ext-moon', time: 2000 });
            return;
        }
        var da = {};
        for (var i = 0; i < list.length; i++) {
            da["productAttrs[" + i + "].ProductId"] = list[i].ProductId;
            da["productAttrs[" + i + "].AttrId"] = list[i].AttrId;
            da["productAttrs[" + i + "].IsInput"] = list[i].IsInput;
            da["productAttrs[" + i + "].AttrValueId"] = list[i].AttrValueId;
            da["productAttrs[" + i + "].Inputvalue"] = list[i].Inputvalue;
        }
        $.ajax({
            type: "POST",
            dataType: 'json',

            url: '@(Domain)/WXProduct/EditProductAttr?productId=' + productid,
            data: da,
            cache: false,
            success: function (data) {
                layer.alert(data.Data, { icon: 1, skin: 'layer-ext-moon', time: 2000 });
            },
            error: function (data) {
                var da = data;
            }
        });
    }
</script>